<?php
require_once "yxxdbconn.php";


$data=array();

//营业总额
$sql = "select '营业总额' as name, SUM(ORDER_PRICE) AS value FROM tb_order WHERE PAY_TYPE=2 OR PAY_TYPE=1";

$query=$mysqli->query($sql);
$res=mysqli_fetch_array($query);

$data['yyze']=number_format($res['value'],2);
//店铺总量
$sql1 = "SELECT '店铺总量' as name,COUNT(*) as value FROM tb_shop_info WHERE DISTRICT  NOT LIKE '%长宁区%'";

$query1=$mysqli->query($sql1);
$res1=mysqli_fetch_array($query1);

$data['dpzl']=number_format($res1['value'],0);


//店铺活跃量
$sql2 = "SELECT '店铺活跃量' AS NAME , COUNT(*) AS VALUE FROM( SELECT DISTINCT SHOP_ID FROM tb_order WHERE HOUR( timediff( now() , FROM_UNIXTIME( ORDER_DATETIME , '%Y-%m-%d %H:%i:%s'))) < 48) AS torder";

$query2=$mysqli->query($sql2);
$res2=mysqli_fetch_array($query2);

$data['dphyl']=number_format($res2['VALUE'],0);

//昨日营业额
$sql3 = "SELECT '昨日营业额' AS NAME , sum(order_price) AS VALUE 
FROM tb_order 
WHERE 
FROM_UNIXTIME(ORDER_DATETIME,'%Y-%m-%d')=date_format(date_sub(now(),interval 1 day),'%Y-%m-%d')   and ORDER_STATE=3 ";

$query3=$mysqli->query($sql3);
$res3=mysqli_fetch_array($query3);

$data['zryye']=number_format($res3['VALUE'],2);


//今日营业额
$sql4 = "SELECT '今日营业额' AS NAME , sum(order_price) AS VALUE 
FROM tb_order 
WHERE 
FROM_UNIXTIME(ORDER_DATETIME,'%Y-%m-%d')=date_format(now(),'%Y-%m-%d')  and ORDER_STATE=3 ";

$query4=$mysqli->query($sql4);
$res4=mysqli_fetch_array($query4);

$data['jryye']=number_format($res4['VALUE'],2);

//昨日订单数
$sql5 = "SELECT '昨日订单数' AS NAME , COUNT(*) AS VALUE 
FROM tb_order 
WHERE 
FROM_UNIXTIME(ORDER_DATETIME,'%Y-%m-%d')=date_format(date_sub(now(),interval 1 day),'%Y-%m-%d')   and ORDER_STATE=3 ";

$query5=$mysqli->query($sql5);
$res5=mysqli_fetch_array($query5);

$data['zrdds']=number_format($res5['VALUE'],0);
//今日订单数
$sql6 = "SELECT '今日订单数' AS NAME , COUNT(*) AS VALUE 
FROM tb_order 
WHERE 
FROM_UNIXTIME(ORDER_DATETIME,'%Y-%m-%d')=date_format(now(),'%Y-%m-%d')  and ORDER_STATE=3 ";

$query6=$mysqli->query($sql6);
$res6=mysqli_fetch_array($query6);

$data['jrdds']=number_format($res6['VALUE'],0);

//当月订单店铺排名
$sql7 = "select  count(*) as orders,CONCAT( SHOP_NAME ,'  (', count(*),'单)') as SHOP_NAME
from tb_order  
where FROM_UNIXTIME(ORDER_DATETIME,'%Y-%m') =date_format(now(),'%Y-%m')
and ORDER_STATE=3
group by SHOP_ID
order by orders desc
limit 5 ";


$query7=$mysqli->query($sql7);

	while ($row7=mysqli_fetch_assoc($query7)){
            $data['dydddppm'][] = $row7;
    }



//当月店铺业绩排名
$sql8 = "SELECT ooo.shop_name,ooo.orderprices,CONCAT(ooo.shop_name,'  ¥',ooo.orderprices) as content
from (
	select  
	count(*) as ordercount,
	sum(order_price) as orderprices,
	SHOP_ID,
	SHOP_NAME 
	from tb_order  
        WHERE  FROM_UNIXTIME(ORDER_DATETIME,'%Y-%m') =date_format(now(),'%Y-%m') and ORDER_STATE=3 
        group by SHOP_ID
	) as ooo
order by ooo.orderprices desc 
limit 5 ";

$query8=$mysqli->query($sql8);

	while ($row8=mysqli_fetch_assoc($query8)){
            $data['dydpyjpm'][] = $row8;
    }


//店铺客单价排名
$sql9 = "SELECT ooo.shop_name,ooo.orderprices,ooo.orderprices/ordercount as value1, ooo.ordercount,CONCAT(ooo.shop_name,' ¥',convert(ooo.orderprices/ordercount,decimal(10,2))) as content
from (
	select  
	count(*) as ordercount,
	sum(order_price) as orderprices,
	SHOP_ID,
	SHOP_NAME 
	from tb_order  
       WHERE  FROM_UNIXTIME(ORDER_DATETIME,'%Y-%m') =date_format(now(),'%Y-%m') and ORDER_STATE=3
        group by SHOP_ID
	) as ooo
order by ooo.orderprices/ordercount desc limit 5";

$query9=$mysqli->query($sql9);

	$i=1;
	while ($row9=mysqli_fetch_assoc($query9)){
		$image="https://file.xykj.com//images/{$i}.png";
		 	$row9['image']=$image;

            $data['dpkdjpm'][] = $row9;

            $i=$i+1;


    }




//打印模式构成

$sql10 = "select COUNT(*) AS y ,(CASE ORDER_TYPE WHEN '1' THEN '预约打印' ELSE '自助打印' END) as x FROM tb_order WHERE (PAY_TYPE=2 OR PAY_TYPE=1) GROUP BY ORDER_TYPE ";
$query10=$mysqli->query($sql10);
$res10=mysqli_fetch_array($query10);

$data['yydy']=number_format($res10['y'],2);

// 预约打印
$sql11 = "select COUNT(*) AS y FROM tb_order ";
$query11=$mysqli->query($sql11);
$res11=mysqli_fetch_array($query11);
$data['all_order']=$res11['y'];

//自助打印
$data['zzdy']=round(($res11['y']-$res10['y']),2);


$data['yydyb']=round((($res10['y']/$res11['y'])*100),2);
$data['zzdyb']=round((($data['zzdy']/$res11['y'])*100),2);

mysqli_close($mysqli); 

echo jsonSuccess($data);




?>